package com.freesky.biz.pay.config;

import com.alibaba.fastjson.JSON;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * 微信支付相关配置
 */
@Configuration
@Data
@ConfigurationProperties(prefix = "wx.pay")
public class WxPayConfiguration {

    private List<PayConfig> configs;

    public List<PayConfig> getConfigs() {
        return configs;
    }

    public void setConfigs(List<PayConfig> configs) {
        this.configs = configs;
    }

    public WxPayConfig getPayConfigs(String mchId){
        for(PayConfig payConfig1:configs){
            if(mchId.equalsIgnoreCase(payConfig1.getMchId())){
                WxPayConfig payConfig = new WxPayConfig();
                payConfig.setAppId(payConfig1.getAppId());
                payConfig.setMchId(payConfig1.getMchId());
                payConfig.setMchKey(payConfig1.getMchKey());
                //payConfig.setSubAppId(this.subAppId);
                //payConfig.setSubMchId(this.subMchId);
                payConfig.setKeyPath(payConfig1.getKeyPath());
                payConfig.setPrivateCertPath(payConfig1.getPrivateCertPath());
                payConfig.setPrivateKeyPath(payConfig1.getPrivateKeyPath());
                payConfig.setApiV3Key(payConfig1.getApiV3Key());
                payConfig.setCertSerialNo(payConfig1.getCertSerialNo());
                return payConfig;
            }
        }
        return null;
    }

    @Data
    public static class PayConfig {
        //@Value("${wx.pay.appId}")
        private String appId;

        //@Value("${wx.pay.mchId}")
        private String mchId;

        //@Value("${wx.pay.mchKey}")
        private String mchKey;

        //@Value("${wx.pay.subAppId}")
        private String subAppId;

        //@Value("${wx.pay.subMchId}")
        private String subMchId;

        //@Value("${wx.pay.keyPath}")
        private String keyPath;

        //@Value("${wx.pay.privateKeyPath}")
        private String privateKeyPath;

        //@Value("${wx.pay.privateCertPath}")
        private String privateCertPath;

        //@Value("${wx.pay.apiV3Key}")
        private String apiV3Key;

        //@Value("${wx.pay.certSerialNo}")
        private String certSerialNo;
    }

    @Bean
    public WxPayConfig payConfig() {
        WxPayConfig payConfig = new WxPayConfig();
        PayConfig payConfig1 = configs.get(0);
        System.out.println(JSON.toJSONString(payConfig1));
        payConfig.setAppId(payConfig1.getAppId());
        payConfig.setMchId(payConfig1.getMchId());
        payConfig.setMchKey(payConfig1.getMchKey());
        //payConfig.setSubAppId(this.subAppId);
        payConfig.setKeyPath(payConfig1.getKeyPath());
        payConfig.setPrivateCertPath(payConfig1.getPrivateCertPath());
        payConfig.setPrivateKeyPath(payConfig1.getPrivateKeyPath());
        payConfig.setApiV3Key(payConfig1.getApiV3Key());
        payConfig.setCertSerialNo(payConfig1.getCertSerialNo());
        return payConfig;
    }

    @Bean
    public WxPayService payService() {
        WxPayService payService = new WxPayServiceImpl();
        payService.setConfig(payConfig());
        return payService;
    }
}

/*
public class WxPayConfiguration {

    @Value("${wx.pay.appId}")
    private String appId;

    @Value("${wx.pay.mchId}")
    private String mchId;

    @Value("${wx.pay.mchKey}")
    private String mchKey;

    @Value("${wx.pay.subAppId}")
    private String subAppId;

    @Value("${wx.pay.subMchId}")
    private String subMchId;

    @Value("${wx.pay.keyPath}")
    private String keyPath;

    @Value("${wx.pay.privateKeyPath}")
    private String privateKeyPath;

    @Value("${wx.pay.privateCertPath}")
    private String privateCertPath;

    @Value("${wx.pay.apiV3Key}")
    private String apiV3Key;

    @Value("${wx.pay.certSerialNo}")
    private String certSerialNo;

    @Bean
    public WxPayConfig payConfig() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(this.appId);
        payConfig.setMchId(this.mchId);
        payConfig.setMchKey(this.mchKey);
        payConfig.setKeyPath(this.keyPath);
        payConfig.setPrivateCertPath(privateCertPath);
        payConfig.setPrivateKeyPath(privateKeyPath);
        payConfig.setApiV3Key(apiV3Key);
        payConfig.setCertSerialNo(certSerialNo);

        return payConfig;
    }

    @Bean
    public WxPayService payService(){
        WxPayConfig wxPayConfig = payConfig();
        WxPayService payService = new WxPayServiceImpl();
        payService.setConfig(wxPayConfig);
        return payService;
    }
}
*/